package com.monolithiot.sso.common.entity;

import com.monolithiot.sso.common.context.Constants;
import com.monolithiot.sso.common.data.Terminable;
import lombok.Data;

import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import java.util.Date;

/**
 * 2018/12/11 Create by 郭文梁
 * Authorization
 * 授权实体类
 *
 * @author 郭文梁
 * @date 2018/12/11
 */
@Data
@Table(name = "tb_authorization")
public class Authorization implements Terminable {
    /**
     * 为指定用户创建手全对象
     *
     * @param user 用户
     * @return 手全对象
     */
    public static Authorization forUser(User user) {
        Authorization authorization = new Authorization();
        authorization.setUserId(user.getId());
        return authorization;
    }

    /**
     * 记录ID
     * id                 INT(10)      NOT NULL AUTO_INCREMENT,
     */
    @Id
    @GeneratedValue(generator = Constants.SQL.GENERATOR_JDBC)
    @Column(name = "id", length = 10, nullable = false)
    private Integer id;
    /**
     * 被授权的用户ID
     * user_id            int(10)      not null
     */
    @Column(name = "user_id", length = 10, nullable = false)
    private Integer userId;
    /**
     * 用户实体对象
     */
    private User user;
    /**
     * 产生的令牌
     * token              VARCHAR(255) NOT NULL
     */
    @Column(name = "token", nullable = false)
    private String token;
    /**
     * 创建时间
     * create_time        DATETIME     NOT NULL
     */
    @Column(name = "create_time", nullable = false)
    private Date createTime;
    /**
     * 最后一次活动时间
     * last_activity_time DATETIME     NOT NULL
     */
    @Column(name = "last_activity_time", nullable = false)
    private Date lastActivityTime;
    /**
     * 有效时间
     * expire_in          INT(5)       NOT NULL
     */
    @Column(name = "expire_in", nullable = false)
    private Integer expireIn;
}
